//
//  ViewController.m
//  科技头条
//
//  Created by 谢裕湖 on 2017/3/8.
//  Copyright © 2017年 谢裕湖. All rights reserved.
//

#import "ViewController.h"
#import "KJTableViewCell.h"
#import "KJModel.h"
#import "YYModel.h"
#import "MJRefresh.h"
@interface ViewController (){
    NSInteger type;
}

@property(nonatomic,strong)NSMutableArray<KJModel *> *dataList;

@property(nonatomic,strong)MJRefreshNormalHeader *header;

@property(nonatomic,strong)MJRefreshAutoNormalFooter *footer;

@end



@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //self.navigationItem.title = @"";
    self.tableView.estimatedRowHeight = 250;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    //下拉
    _header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    self.tableView.mj_header = _header;
    
    
    //上拉刷新
    _footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    self.tableView.mj_footer = _footer;
    
    [self loadData];
}

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == _dataList.count - 1) {
        
        type = 1;
        [self loadData];
    }
    NSLog(@"%zd--%zd",indexPath.row,_dataList.count-1);
}

//- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
//    
//    if (indexPath.row == _dataList.count - 1) {
//        
//        type = 1;
//        [self loadData];
//    }
//    NSLog(@"%zd--%zd",indexPath.row,_dataList.count-1);
//}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
//    NSLog(@"%zd",_dataList.count);
    return _dataList.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    KJTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.model = _dataList[indexPath.row];
    
    return cell;
}

-(void)loadData{
    
    NSString *time = @"1488941574";
    
    //下拉刷新
    if (type == 0) {
        type = 0;
        //判断初始的时候
        time = _dataList.count > 0 ? [_dataList firstObject].addtime : @"1488941574";
    }
    if (type == 1) {
        
        type = 1;
        time = _dataList.count > 0 ? [_dataList lastObject].addtime : @"1488941574";
    }
    NSLog(@"%@",time);
    //获取网络资源路径
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://news.coolban.com/Api/Index/news_list/app/2/cat/0/limit/20/time/%@/type/%ld",time,type]];
    //获取网络请求
    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:30];
    //发送
    [[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        if (error != nil) {
            NSLog(@"出错了");
        }
        //解析json,反序列化
        NSArray *arrData = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        //字典转模型
        //NSLog(@"%@",arrData);
        
        NSArray *arr = [NSArray yy_modelArrayWithClass:[KJModel class] json:arrData];
        NSMutableArray *arrM = [NSMutableArray arrayWithArray:arr];
//        [_dataList addObjectsFromArray:arrM];
        if (type == 0) {
            _dataList = arrM;
        }else{
            [_dataList addObjectsFromArray:arrM];
        }
        
        //回到主线程更新UI
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            if (type == 0) {
                
                [_header endRefreshing];
            }else{
                [_footer endRefreshing];
                type = 0;
            }
            
            [self.tableView reloadData];
        }];
        
    }]resume];
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
